#ifndef PLAYER_CONTROLS_H
#define PLAYER_CONTROLS_H

#include "widget.h"
#include "../../anime/utils.h"

#include <QPainterPath>
#include <QRadialGradient>
#include <QLinearGradient>

class PlayerControls;

class PlayerControlsListener
{
public:
    virtual void OnControlClick( PlayerControls* sender, int controlId ) = 0;
    virtual void OnVolumeChange( PlayerControls* sender, float vol ) = 0;
};

class PlayerControls: public Widget, private AnimationObserver
{
public:
    static int CTRL_PREV_SEGMENT;
    static int CTRL_PLAY;
    static int CTRL_STOP;
    static int CTRL_NEXT_SEGMENT;
    static int CTRL_PAUSE;

    PlayerControls(Widget* _parent, Engine* _engine, int _x, int _y, int _w, int _h);
    virtual ~PlayerControls();

    virtual void Paint( QPainter& painter );

    void AddPlayerControlsListener( PlayerControlsListener* pcl );
    void Init( void );

    virtual void OnMouseMoved( QMouseEvent *e, bool onWidget, int x_rel, int y_rel );
    virtual void OnMousePressed( QMouseEvent *e, bool inside, int x_rel, int y_rel );
    virtual void OnMouseReleased( QMouseEvent *e, bool inside, int x_rel, int y_rel );

    float volPos;
    bool playing;

private:
    QList< PlayerControlsListener* > listeners;

    QPainterPath cloud;
    QPainterPath smallSelection;
    QPainterPath bigSelection;
    QBrush* cloudBrush;
    QBrush* selectionBrush;
    QBrush* pressedBrush;

    QPainterPath volPath;
    QPixmap* volPixmap;
    QPixmap* volImgPixmap;
    QRect* volSliderRect;
    int w6;
    float m;
    int prevVolX;
    float prevVolPos;
    bool volDragging;
    QBrush* backTriangleBrush;

    QRadialGradient* cloudGradient;

    QPixmap* playPixmap;
    QPixmap* pausePixmap;
    QPixmap* nextSegmentPixmap;
    QPixmap* prevSegmentPixmap;
    QPixmap* stopPixmap;

    QRect* prevRect;
    QRect* playRect;
    QRect* stopRect;
    QRect* nextRect;
    QRect* volRect;
    QRect* rectArray[5];

    int controlSelected;
    int controlPressed;

    int h2;

    int stopX;
    int stopOffset;     // center-left offset

    int playX;
    int playOffset;

    int nextX;
    int nextOffset;

    int prevX;
    int prevOffset;

    int volX;

    IntGraph animeGraph;
    Animation stopAnime;
    Animation prevAnime;
    Animation playAnime;
    Animation nextAnime;
    Animation* animeArray[4];
    virtual void OnPause(Animation* a);
    virtual void OnStop(Animation* a);
    virtual void OnEnd(Animation* a);
    virtual void OnPlay(Animation* a);

    void DrawCloud();
    void InitializeVolumeStuff();
    inline void DrawControl( QPainter& p, QPixmap& pixmap, int& x, int& offset );
    inline void TriggerEvent( bool vol );
};

#endif // PLAYER_CONTROLS_H
